import scrapy
import re
from ccmk.items import HighVoltageItem
# from ccmk.items import SensorOverrunItem
import json
from scrapy.http import Request


class ZdhbbSpider(scrapy.Spider):
    name = 'zdhbb'
    # allowed_domains = ['www.nnn.com']
    start_urls = [
        'file:///C:/Users/gpf_win10/Desktop/%E5%8E%86%E5%8F%B2%E7%BB%9F%E8%AE%A1%20--%20%E6%A8%A1%E6%8B%9F%E9%87%8F%E6%8A%A5%E8%AD%A6_files/biandian.html']
    moudle_urls=[]
    url1_text=['file:///C:/Users/gpf_win10/Desktop/%E5%8E%86%E5%8F%B2%E7%BB%9F%E8%AE%A1%20--%20%E6%A8%A1%E6%8B%9F%E9%87%8F%E6%8A%A5%E8%AD%A6_files/%E5%8E%86%E5%8F%B2%E7%BB%9F%E8%AE%A1%20--%20%E6%A8%A1%E6%8B%9F%E9%87%8F%E6%8A%A5%E8%AD%A63.html',
               'file:///C:/Users/gpf_win10/Desktop/%E5%8E%86%E5%8F%B2%E7%BB%9F%E8%AE%A1%20--%20%E6%A8%A1%E6%8B%9F%E9%87%8F%E6%8A%A5%E8%AD%A6_files/%E5%8E%86%E5%8F%B2%E7%BB%9F%E8%AE%A1%20--%20%E6%A8%A1%E6%8B%9F%E9%87%8F%E6%8A%A5%E8%AD%A6.html',
               'file:///C:/Users/gpf_win10/Desktop/%E5%8E%86%E5%8F%B2%E7%BB%9F%E8%AE%A1%20--%20%E6%A8%A1%E6%8B%9F%E9%87%8F%E6%8A%A5%E8%AD%A6_files/%E5%8E%86%E5%8F%B2%E7%BB%9F%E8%AE%A1%20--%20%E6%A8%A1%E6%8B%9F%E9%87%8F%E6%8A%A5%E8%AD%A61.html',
               'file:///C:/Users/gpf_win10/Desktop/%E5%8E%86%E5%8F%B2%E7%BB%9F%E8%AE%A1%20--%20%E6%A8%A1%E6%8B%9F%E9%87%8F%E6%8A%A5%E8%AD%A6_files/%E5%8E%86%E5%8F%B2%E7%BB%9F%E8%AE%A1%20--%20%E6%A8%A1%E6%8B%9F%E9%87%8F%E6%8A%A5%E8%AD%A62.html']
    url2_text=['file:///C:/Users/gpf_win10/Desktop/%E5%8E%86%E5%8F%B2%E7%BB%9F%E8%AE%A1%20--%20%E6%A8%A1%E6%8B%9F%E9%87%8F%E6%8A%A5%E8%AD%A6_files/%E5%8E%86%E5%8F%B2%E7%BB%9F%E8%AE%A1-%E6%95%85%E9%9A%9C%E7%BB%9F%E8%AE%A15.html']

    def parse(self, response):
        for _each in response.xpath('/html/body/ul'):
            # print(_each)
            sensor_early = _each.xpath('./li//text()').getall()
            # print(sensor_early)
            if sensor_early == None:
                continue
            else:

                if len(sensor_early) > 3:
                    item = HighVoltageItem()
                    item['sensor_id']=_each.xpath('./@id').get().split('-')[-1]
                    print((item['sensor_id']))
                    # sensor_id=_each.xpath('./@id').get().split('-')[-1]
                    item['sensor_name'] = sensor_early[0]  # 传感器地点
                    item['sensor_up'] = sensor_early[3]  # 断电值
                    item['sensor_restart'] = sensor_early[5]  # 复电值
                    high_voltage_switchgear_re = re.compile(r'(\d+#)高开')  # 高开正则
                    item['high_voltage_switchgear'] = (
                        ''.join(list(set(high_voltage_switchgear_re.findall(str(sensor_early))))))
                    # print(sensor_early)

                    high_voltage_no_re= re.compile(r'\（(.*?变电所).*?\）')
                    high_voltage_no1=(high_voltage_no_re.findall(str(sensor_early)))
                    high_voltage_no1.append('煤仓远程控制馈电')
                    if item['sensor_name']=='S5.23轨顺后溜机尾甲烷':
                        item['high_voltage_no']=['S5延伸机头变电所']
                    else:
                        item['high_voltage_no']=high_voltage_no1[0]
                    # print(item['high_voltage_no'])
                    # if high_voltage_no1 != None:
                    #     high_voltage_no=high_voltage_no1
                    # else:
                    #     high_voltage_no=


                    # print(sensor_name,sensor_up,high_voltage_no)
                    # print(sensor_early)
                    yield item
        for a22 in self.url1_text:
            yield scrapy.Request(url=a22,callback=self.sensor_overrun,meta={'item':item})

            # \)(.*?)高开 |(\d+#)高开   电\（|态\((.*?变电所)

            # high_voltage_no_re= re.compile(r'\((.*?变电所)|\（(.*?变电所)')
                        # item['high_voltage_no']=(high_voltage_no_re.findall(str(sensor_early)))
                    # print(sensor_name,sensor_up,high_voltage_no)
                # \)(.*?)高开 |(\d+#)高开     电\（|态\((.*?变电所)
    #获取下一页
    # yield Request(response.request.url,meta={'next_page':True}) #t同一个页面下的请求

    def sensor_overrun(self,response):
        item = response.meta['item']
        i44=response.xpath('//*[@id="CallbackPanel_grid1_DXMainTable"]//tr/td[@class="dxgv dx-ac"]')
        for i in i44:
            # print(i,type(i))
            m=i.xpath('./div/@onclick').get()

            a1_re=re.compile(r'showAlarmDetail\(this,\[(.*?)\,\{')
            a1=a1_re.findall(str(m))
            if a1 != []:

                a11=json.loads(a1[0])

                item['MonitorCode']=a11['MonitorCode']   #传感器内部id
                print(type(item['MonitorCode']))
                # if item['MonitorCode']==item['sensor_id']:
                # itemMonitorCode=a11['MonitorCode']
                item['FixedLocation'] = a11['FixedLocation']  # 地点名称
                item['sensor_type'] =i.xpath('../td[3]/text()').get()  #传感器类型
                item['AlarmMark']=i.xpath('../td[4]/text()').get() #报警类型
                item['AlarmStartTime']=i.xpath('../td[5]/text()').get().split(' ')[1] #开始报警时间
                item['AlarmDate']= i.xpath('../td[5]/text()').get().split(' ')[0]    #报警日期
                item['AlarmEndTime']=i.xpath('../td[6]/text()').get().split(' ')[1]  #结束报警时间
                item['MaxValue']=i.xpath('../td[8]/text()').get().split('(')[0]  #报警最大值
                item['AvgValues']=i.xpath('../td[9]/text()').get().split('(')[0]  #报警平均值
                item['MinValues']=i.xpath('../td[10]/text()').get().split('(')[0]  #报警最小值
                item['AlarmDuration']=i.xpath('../td[7]/text()').get().rsplit(':',1)[0] #持续时间

                yield item
        # for i1 in self.url2_text:
        #     yield scrapy.Request(url=i1, callback=self.sensor_fault, meta={'item': item})


               # print(MaxValueStr,AvgValues,MinValues,FixedLocation)

    def sensor_fault(self,response):
        pass
    #     for _each in (response.xpath('//tr[@class="dxgvGroupRow_DevEx]"'))
    #         sensor_fault_early=_each.xpath('./td[2]/text()').get()
    #         sensor_fault_no=_each.xpath('./@id').get()
    #         sensor_fault_MonitorCode=''.join(re.findall(re.compile('编号: (.+?)  '),str(sensor_fault_early)))
    #         sensor_fault_FixedLocation = ''.join(re.findall(re.compile('地点:(.+?)\('), str(sensor_fault_early)))
    #         sensor_fault_error_no = ''.join(re.findall(re.compile('累计故障次数:(\d+)'), str(sensor_fault_early)))
    #         sensor_fault_type_no=''.join(re.findall(re.compile('CallbackPanel_grid_DXGroupRowExp(\d+)'), str(sensor_fault_no)))
    #         #编号: (.+?)   地点:(.+?)\(   累计故障次数:(\d+)  CallbackPanel_grid_DXGroupRowExp(\d+)
    #
    #
    #         print(sensor_fault_MonitorCode,sensor_fault_FixedLocation,sensor_fault_error_no,sensor_fault_type_no)


